package com.zwj.learning.timetable.service.impl;

import cn.hutool.core.util.StrUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.read.listener.PageReadListener;
import com.zwj.common.service.UserService;
import com.zwj.learning.timetable.common.TokenThreadLocal;
import com.zwj.learning.timetable.constant.RedisKey;
import com.zwj.learning.timetable.entity.Course;
import com.zwj.learning.timetable.entity.TimeTable;
import com.zwj.learning.timetable.mapper.CourseMapper;
import com.zwj.learning.timetable.service.CourseService;
import com.zwj.learning.timetable.service.TimetableExcelService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

@Service
public class TimetableExcelServiceImpl implements TimetableExcelService {

    @Resource
    private CourseService courseService;

    @DubboReference
    private UserService userService;

    @Override
    public Object upload(MultipartFile file, Integer semesterId) throws IOException {

        final List<Course> courses = new ArrayList<>();

        List<String> courseNames = new ArrayList<>();

        InputStream inputStream = file.getInputStream();

        EasyExcel.read(inputStream, TimeTable.class,
                new PageReadListener<TimeTable>(dataList -> {

                    for (int i = 2; i <= 6; i++) {

                        TimeTable timeTable = dataList.get(i);
                        String time = i - 1 + "";
                        weekHandler(timeTable.getWeek1(), courses, time, courseNames, "1",semesterId);
                        weekHandler(timeTable.getWeek2(), courses, time, courseNames, "2",semesterId);
                        weekHandler(timeTable.getWeek3(), courses, time, courseNames, "3",semesterId);
                        weekHandler(timeTable.getWeek4(), courses, time, courseNames, "4",semesterId);
                        weekHandler(timeTable.getWeek5(), courses, time, courseNames, "5",semesterId);
                        weekHandler(timeTable.getWeek6(), courses, time, courseNames, "6",semesterId);
                        weekHandler(timeTable.getWeek7(), courses, time, courseNames, "7",semesterId);

                    }
                })).sheet().doRead();
        boolean b = courseService.saveBatch(courses);

        if (!b) return "导入失败!";

        return "导入成功!";
    }

    public void weekHandler(String week, List<Course> courses, String time,
                            List<String> courseNames,
                            String weekId,Integer semesterId) {

        if (StrUtil.isNotBlank(week)) {
            String[] classes = week.split("\n\n");

            for (String classO : classes) {

                String[] classInfo = classO.split("\n");

                Course course = new Course();

                List<String> classInfos = Arrays.asList(classInfo);

                course.setCourseName(classInfos.get(0));

                course.setCourseTeacher(classInfos.get(1));

                String semesterWeekBefore = StrUtil.subBefore(classInfos.get(2), "(", false);

                List<String> semesterWeeks =
                        Arrays.asList(semesterWeekBefore.split("-"));
                StringBuilder semesterWeek = new StringBuilder();
                for (int j = Integer.parseInt(semesterWeeks.get(0));
                     j <= Integer.parseInt(semesterWeeks.get(1)); j++) {

                    if (j == Integer.parseInt(semesterWeeks.get(1))) {
                        semesterWeek.append(j);
                        break;
                    }
                    semesterWeek.append(j).append(",");
                }

                course.setSemesterWeekIds(semesterWeek.toString());
                course.setCourseAddress(classInfos.get(3));
                course.setTimeIds(time);
                course.setWeekIds(weekId);

                if (courseNames.contains(course.getCourseName())) {

                    Course course1 = courses.get(courseNames.indexOf(course.getCourseName()));
                    course1.setTimeIds(course1.getTimeIds() + "," + time);
                    course1.setWeekIds(course1.getWeekIds() + "," + weekId);
                    return;
                } else courseNames.add(course.getCourseName());

                //  添加属于人
                String currentToken = TokenThreadLocal.getCurrentToken();
                Long loginUserId = userService.getLoginUserId(currentToken);
                course.setStudentId(loginUserId);
                course.setSemesterId(semesterId);

                courses.add(course);
            }
        }
    }


}
